﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;


public partial class AgentList : System.Web.UI.Page
{
    public string Name { get; set; }
    public string Addressline1 { get; set; }
    public string Addressline2 { get; set; }
    public string Tel { get; set; }
    public string Fax { get; set; }
    public string Email { get; set; }
    public string SortField { get; set; }
    public string SortOption { get; set; }
    public string GuestID { get; set; }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
            {
                return int.Parse(Request["page"].ToString());
            }
            return 1;
        }
    }
    public int PageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetPropertyFromQueryString();
            Search();
        }
    }
    void GetPropertyFromQueryString()
    {
        if (Request["GuestID"] != null)
            GuestID = Request["GuestID"];
        else
            GuestID = string.Empty;
        txtGuestID.Text = GuestID;
        if (Request["Fax"] != null)
            Fax = Request["Fax"];
        else
            Fax = string.Empty;
        txtFax.Text = Fax;
        if (Request["Tel"] != null)
            Tel = Request["Tel"];
        else
            Tel = string.Empty;
        txtEmail.Text = Email;
        if (Request["Email"] != null)
            Email = Request["Email"];
        else
            Email = string.Empty;
        txtTel.Text = Tel;
        if (Request["Addressline1"] != null)
            Addressline1 = Request["Addressline1"];
        else
            Addressline1 = string.Empty;
        txtAddressLine1.Text = Addressline1;
        if (Request["Addressline2"] != null)
            Addressline2 = Request["Addressline2"];
        else
            Addressline2 = string.Empty;
        txtAddressLine1.Text = Addressline1;
        if (Request["Name"] != null)
            Name = Request["Name"].ToString();
        else
            Name = string.Empty;
        txtGuestName.Text = Name;


        //if (Request["SortField"] == null)
        //    SortField = ddlSortField.SelectedValue;
        //else
        //{
        //    SortField = Request["SortField"].ToString();
        //    ddlSortField.SelectedValue = SortField;
        //}
        //if (Request["SortOption"] == null)
        //    SortOption = ddlOption.SelectedValue;
        //else
        //{
        //    SortOption = Request["SortOption"].ToString();
        //    ddlOption.SelectedValue = SortOption;
        //}

        //PageIndex = Request["Page"] == null ? 1 : int.Parse(Request["Page"].ToString());
    }
    void GetPropertyFromForm()
    {
        Addressline1 = txtAddressLine1.Text;
        Addressline2 = txtAddressLine2.Text;
        Email = txtEmail.Text;
        Fax = txtFax.Text;
        GuestID = txtGuestID.Text;
        Name = txtGuestName.Text;
        Tel = txtTel.Text;
    }
    void Search()
    {
        HashSet<string> hs = new HashSet<string>();
        //hs.Add(SortField + " " + SortOption);
        int count = 0;
        grv.DataSource = GuestDAO.CreateInstant().SelectListItem(GuestID, Name, Email, Addressline1, Addressline2, Tel, Fax, PageIndex, PageSize, out count, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = count;
        smartPager.UrlFormatString = string.Format(@"/Pages/Agents/AgentList.aspx?Name={0}&Addressline1={1}&Addressline2={2}&Tel={3}&Fax={4}&Email={5}&GuestID={6}&SortField={7}&SortOption={8}", Name, Addressline1, Addressline2, Tel, Fax, Email, GuestID, SortField, SortOption) + "&page={0}";
        smartPager.Visible = count > PageSize;
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        GetPropertyFromForm();
        Response.Redirect(string.Format(@"/Pages/Agents/AgentList.aspx?Name={0}&Addressline1={1}&Addressline2={2}&Tel={3}&Fax={4}&Email={5}&GuestID={6}&SortField={7}&SortOption={8}", Name, Addressline1, Addressline2, Tel, Fax, Email, GuestID, SortField, SortOption));
    }
}
